package com.hxm.webview;

import android.test.InstrumentationTestCase;
import android.util.Log;

import com.hxm.webview.api.Api;
import com.hxm.webview.api.HttpUrl;
import com.hxm.webview.api.NewsDetailApi;
import com.hxm.webview.entity.NewsDetail;
import com.hxm.webview.entity.WeChat;
import com.hxm.webview.http.HttpRequest;
import com.hxm.webview.http.listener.RequestListener;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by HXM on 2016/8/8.
 * Description:
 */
public class MyTest extends InstrumentationTestCase {

    public void testNewsDetailApi(){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(HttpUrl.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        NewsDetailApi detailApi=retrofit.create(NewsDetailApi.class);
        detailApi.getNewsDetail("484391")
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .flatMap(new Func1<NewsDetail[], Observable<NewsDetail>>() {
                    @Override
                    public Observable<NewsDetail> call(NewsDetail[] newsDetails) {
                        return Observable.from(newsDetails);
                    }
                }).subscribe(new Subscriber<NewsDetail>() {
            @Override
            public void onCompleted() {
                Log.i("news","onCompleted()");
            }

            @Override
            public void onError(Throwable e) {
                Log.i("news",e+"");
            }

            @Override
            public void onNext(NewsDetail newsDetail) {
                Log.i("news",newsDetail.getEditor());
                System.out.print(newsDetail.getContent());
            }
        });
    }
    public void testWeChat(){
        HttpRequest request=new HttpRequest(Api.WECHAT_BASE_URL);
        request.getWeChatData("1", new RequestListener<WeChat>() {
            @Override
            public void success(WeChat data) {
               System.out.print(data.getReason());
            }

            @Override
            public void failed(String error) {
                System.out.print(error);
            }
        });
    }
}
